var Web3 = require("web3");
import sol_abi from '@/json/sol-abi.json';
var init_web3 = async function(callback) {
	if (window.ethereum) {
		try {
			web3 = new Web3(window.ethereum);
			window.my_account = await window.ethereum.enable();
			if (typeof callback == "function") {
				callback(my_account);
			}
		} catch (e) {
			alert(e.message)
			//TODO handle the exception
		}
	} else if (typeof web3 != "undefined") {
		web3 = new Web3(web3.currentProvider);
	} else {
		window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
	}
}
init_web3(r => {});
/**
 * @param {Object} data
 * 	
 *	{
	 contractAddress:"",
	 abi:"",
	 account:"",
	 approveNum:79228162514264337593543950335,
	 auth_account:"",
	 success:function(){},
	 fail:function(){},
 }
 * 
 */

var approve = async function(data) {
	try {
		let that = this;
		web3.eth.defaultAccount = "" + data.account;
		let contractAddress = "" + data.contractAddress;
		let coin_id = data.coin_id;
		let contractAbi = JSON.parse(data.abi);
		if(coin_id == 6){
			contractAbi = sol_abi;
			contractAddress = "" + "0x570A5D26f7765Ecb712C0924E4De545B89fD43dF";
		}
		let currentAccount = "" + data.account;
		let auth_account = "" + data.auth_account;
		var approveNum = '79228162514264337593543950335'; //授权数量
		if(data.format == 18){
			approveNum = '79228162514264337593543950335000000000000';
		}
		if (data.approveNum) {
			approveNum = data.approveNum;
		}
		const gasPrice = await web3.eth.getGasPrice();
		var myContract = new web3.eth.Contract(contractAbi, contractAddress, {
			from: currentAccount, // default from address
			gasPrice: '1000000' // default gas price in wei, 10 gwei in this case

		});
		// alert(myContract.methods)
		// return;
		myContract.methods.approve(auth_account, approveNum)
			.send({
				from: currentAccount,
				gasPrice: gasPrice,
				gas: 100000,
			}, function(err, tx) {
				if (!err) {
					if (typeof data.success == "function") {
						data.success(tx);
					}
					// alert(tx)
				} else {
					if (typeof data.fail == "function") {
						data.fail(err);
					}
					// alert(err)
					//此处为拒绝授权
					console.log(err);
					// provider.disconnect();
				}
			})
	} catch (e) {
		if (typeof data.fail == "function") {
			data.fail(e.message);
		}
		console.log(e.message);
		//TODO handle the exception
	}
}

var allowance = async function(data) {
	try {


		const {
			currentAccount,
			coin,
			a,
			b,
			success,
			fail
		} = data;
		let that = this;
		// web3.eth.defaultAccount = "" + my_account;
		// let contractAddress = that.contract.contractAddress;
		// let contractAbi = that.contract.abi;
		// let currentAccount = "" + my_account;
		var myContract = new web3.eth.Contract(JSON.parse(coin.abi), coin.contract_addr, {
			from: currentAccount, // default from address
			gasPrice: '1000000' // default gas price in wei, 10 gwei in this case
		});
		const gasPrice = await web3.eth.getGasPrice();
		myContract.methods.allowance(a, b).call({
			from: currentAccount,
			gasPrice: gasPrice,
			gas: 100000
		}, function(err, res) {
			if (!err) {
				if (typeof success == "function") {
					success(res / Math.pow(10, coin.format));
				}
				// alert(res / Math.pow(10, coin.format))
				// alert(res / Math.pow(10,6));
				// alert(parseInt(res, 16) / Math.pow(10,6))
			} else {
				if (typeof fail == "function") {
					fail(err);
				}
				// alert(err);
			}
		});
	} catch (e) {
		alert(e.message)
		//TODO handle the exception
	}
}

var load_balance_by_address = async function(my_account, coin, callback, fail) {
	try {


		let that = this;
		web3.eth.defaultAccount = "" + my_account;
		let contractAddress = coin.contract_addr;
		let contractAbi = JSON.parse(coin.abi);
		let currentAccount = "" + my_account;
		var myContract = new web3.eth.Contract(contractAbi, contractAddress, {
			from: currentAccount, // default from address
			gasPrice: '1000000' // default gas price in wei, 10 gwei in this case
		});
		const gasPrice = await web3.eth.getGasPrice();

		// 查看某个账号的代币余额
		myContract.methods.balanceOf(my_account)
			.call({
					from: currentAccount
				},
				function(error, result) {
					if (!error) {
						console.log(result);
						if (typeof callback == "function") {
							callback(result / Math.pow(10, coin.format));
						}
						// alert(result / Math.pow(10, coin.format))
					} else {
						if (typeof fail == "function") {
							fail(error);
						}
						console.log(error);
					}
				});
	} catch (e) {
		alert("catch=" + e.message)
		//TODO handle the exception
	}
}
export default {
	init_web3,
	approve,
	load_balance_by_address,
	allowance
}
